using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BaseUIController : MonoBehaviour
{
	private static BaseUIController m_Instance;

	[SerializeField]
	private Camera m_UICamera;

	[SerializeField]
	private TextArea m_Text;

	[SerializeField]
	private OperateArea m_CueOperateArea;

	[SerializeField]
	private FireSlider m_FireSlider;

	[SerializeField]
	private CueAndGuidelines m_CueAndLines;

	[SerializeField]
	private Siding m_Siding;

	[SerializeField]
	private Transform m_TopMenuRoot;

	[SerializeField]
	private GameObject m_btnLevelInfo;

	[SerializeField]
	[HideInInspector]
	private TopMenu m_TopMenu;

	[SerializeField]
	private SettlementUIManager m_Settlement;

	[SerializeField]
	private TextTips m_Tips;

	[SerializeField]
	private GameObject m_GlobalMask;

	[SerializeField]
	private Animator m_GlobalBlackMask;

	[SerializeField]
	private BallInHand m_Hand;

	[SerializeField]
	private GameObject m_FadeMask;

	[SerializeField]
	private GameObject m_RiseMask;

	[SerializeField]
	private Button m_MenuToggle;

	[SerializeField]
	public Image targetPocketImage;

	[SerializeField]
	public Image targetBallImage;

	[SerializeField]
	public Image hitPointImage;

	[SerializeField]
	public Image newHitPointImage;

	[SerializeField]
	public bool debugAI;

	[SerializeField]
	public UIToolBar itemToolBar;

	public GameObject pauseMenu;

	public RectTransform nativeAdPoint;

	private bool m_MenuHide;

	private bool m_IsSmallBannerShowing;

	private List<TextTips> m_TipsCache = new List<TextTips>();

	private int ingameAdCounter;

	private bool _bShowIngameAd;

	private bool _bShowNative;

	private float _nativePos;

	public bool menuHide => m_MenuHide;

	public static BaseUIController Instance => m_Instance;

	public static UIToolBar itemToolbar => m_Instance.itemToolBar;

	public static TextArea text => m_Instance.m_Text;

	public static OperateArea cueOperateArea => m_Instance.m_CueOperateArea;

	public static FireSlider fireSlider => m_Instance.m_FireSlider;

	public static CueAndGuidelines cueAndLines => m_Instance.m_CueAndLines;

	public static Siding siding => m_Instance.m_Siding;

	public static Transform TopMenuRoot => m_Instance.m_TopMenuRoot;

	public static SettlementUIManager MSettlement => m_Instance.m_Settlement;

	public static bool GlobalMask
	{
		get
		{
			return m_Instance.m_GlobalMask.activeInHierarchy;
		}
		set
		{
			cueAndLines.SetFade(value);
			m_Instance.m_GlobalMask.SetActive(value);
		}
	}

	public static BallInHand hand => m_Instance.m_Hand;

	public static TopMenu topMenu
	{
		get
		{
			return m_Instance.m_TopMenu;
		}
		set
		{
			m_Instance.m_TopMenu = value;
		}
	}

	private void Awake()
	{
		if ((bool)m_Instance)
		{
			UnityEngine.Debug.LogError("two " + base.gameObject.name + " in the scene. check the code");
		}
		m_Instance = this;
		GameManager.CurrentUIRoot = GetComponent<Canvas>();
		m_Text.gameObject.SetActive(value: false);
		m_FadeMask.SetActive(value: true);
		PoolRulesBase.onFireBall = (Delegate0Args)Delegate.Combine(PoolRulesBase.onFireBall, new Delegate0Args(OnFireBall));
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Combine(PoolRulesBase.onNewTurn, new Delegate1Args<int>(OnStartRound));
		m_MenuHide = true;
		ConstantData.watchedAdAtThisBattle = false;
		if (!debugAI)
		{
			targetPocketImage.gameObject.SetActive(value: false);
			targetBallImage.gameObject.SetActive(value: false);
			hitPointImage.gameObject.SetActive(value: false);
			newHitPointImage.gameObject.SetActive(value: false);
		}
	}

	private void Start()
	{
		int num = -1;
		int num2 = -1;
		if (GameManager.GType == GameType.Mission)
		{
			UnityEngine.Debug.Log("IS MISSION!");
			string fileName = LevelDataIndex.CurrentLevel.FileName;
			num = int.Parse(fileName.Split('-')[0]);
			num2 = int.Parse(fileName.Split('-')[1]);
			if (num > 1)
			{
				Advertising.TryPopIngameAd();
			}
			m_btnLevelInfo.SetActive(value: true);
		}
		else
		{
			Advertising.TryPopIngameAd();
		}
		pauseMenu.SetActive(value: false);
		if ((num != 1 || num2 != 1) && AdManagerOld.instance.curAd.HasInterstitialGift(QiXunAd.PAGE_MAIN))
		{
			UIADViewWnd.ShowGift("Gift", delegate
			{
			});
		}
	}

	private void OnEnable()
	{
		ingameAdCounter = 0;
	}

	private void Update()
	{
		if (UnityEngine.Input.GetKeyDown(KeyCode.Escape) && !UIADViewWnd.EscapeGiftWnd())
		{
			if ((bool)base.transform.Find("TextDialog"))
			{
				base.transform.GetChild(base.transform.childCount - 1).GetComponent<TextDialog>().OnCancel();
			}
			else if ((bool)RateThisApp.instance)
			{
				RateThisApp.instance.Later();
			}
			else if (m_Settlement.WindowOpen())
			{
				m_Settlement.Back();
			}
			else if (!base.transform.Find("Tutorial"))
			{
				OnMenuButtonClick();
			}
		}
	}

	private void OnDestroy()
	{
		PoolRulesBase.onFireBall = (Delegate0Args)Delegate.Remove(PoolRulesBase.onFireBall, new Delegate0Args(OnFireBall));
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Remove(PoolRulesBase.onNewTurn, new Delegate1Args<int>(OnStartRound));
		m_Instance = null;
		GameStatistics.Serialize();
	}

	private void OnFireBall()
	{
		GlobalMask = true;
	}

	private void OnStartRound(int playerIndex)
	{
		GlobalMask = false;
	}

	public void OnMenuButtonClick()
	{
		UnityEngine.Debug.Log("OnMenuButtonClick");
		if (!m_MenuHide)
		{
			UnityEngine.Debug.Log("Resume game");
			resumeGame();
			SetADBar(show: false);
			m_MenuHide = true;
			m_GlobalBlackMask.SetTrigger("Hide");
			return;
		}
		UnityEngine.Debug.Log("Pause game");
		pauseGame();
		ingameAdCounter++;
		if (ingameAdCounter >= 2)
		{
			ingameAdCounter = 0;
		}
		_bShowIngameAd = true;
		SetADBar(show: true);
		m_MenuHide = false;
		m_GlobalBlackMask.SetTrigger("Show");
	}

	public static Camera GetUICamera()
	{
		return m_Instance.m_UICamera;
	}

	public static Transform RootTransform()
	{
		return m_Instance.transform;
	}

	public void SetADBar(bool show)
	{
		if (show)
		{
			int num = Mathf.FloorToInt((float)Screen.width * 0.9f);
			int num2 = Mathf.FloorToInt((float)Screen.height * 0.45f);
			Transform transform = pauseMenu.transform.Find("Exit");
			Image component = transform.GetComponent<Image>();
			Vector3 vector = GetUICamera().WorldToScreenPoint(transform.position);
			int num3 = Mathf.FloorToInt((float)Screen.width * 0.05f);
			int num4 = Mathf.FloorToInt(vector.y - (float)num2 - (component.rectTransform.rect.height + 5f) * component.canvas.scaleFactor);
			if (_nativePos <= 0f)
			{
				Vector2 anchoredPosition = nativeAdPoint.anchoredPosition;
				_nativePos = anchoredPosition.y * nativeAdPoint.root.GetComponent<Canvas>().scaleFactor;
			}
			AdManagerOld.instance.ShowInterstitial(QiXunAd.PAGE_PAUSE, AdManagerOld.INTERSECT_CUSTOM_PAUSE, null);
			AdManagerOld.instance.curAd.ShowBanner(QiXunAd.TOP);
		}
		else
		{
			AdManagerOld.instance.curAd.HideNative();
			AdManagerOld.instance.curAd.HideBanner();
		}
	}

	public void Back2MainScene()
	{
		resumeGame();
		AdManagerOld.instance.curAd.HideNative();
		AdManagerOld.instance.curAd.HideBanner();
		if (GameManager.GType == GameType.QuickFire)
		{
			ConstantData.BattleSceneType = string.Empty;
		}
		else
		{
			AdManagerOld.instance.curAd.ShowBanner(QiXunAd.BOTTOM);
		}
		m_RiseMask.GetComponent<AnimationTools>().sceneIndex = 1;
		m_RiseMask.gameObject.SetActive(value: true);
		if (ConstantData.onMaskRiseup != null)
		{
			ConstantData.onMaskRiseup();
		}
	}

	public void RestartScene()
	{
		resumeGame();
		AdManagerOld.instance.curAd.HideNative();
		AdManagerOld.instance.curAd.HideBanner();
		if (GameManager.GType == GameType.Mission)
		{
			if (ConstantData.physical < LayoutConfiguration.instance[LevelDataIndex.CurrentLevel.FileName].PhysicalStrengh)
			{
				if (AdManagerOld.instance.curAd.HasVideo())
				{
					TextDialog.Show2Button(HOLocalizationConfiguration.GetValue(177), delegate
					{
						AdManagerOld.instance.PlayVideo(1, delegate
						{
							ConstantData.vedioPhysicalPowerCount++;
						});
					});
				}
				else
				{
					TextDialog.Show(HOLocalizationConfiguration.GetValue(340));
				}
				return;
			}
			ConstantData.physical -= LayoutConfiguration.instance[LevelDataIndex.CurrentLevel.FileName].PhysicalStrengh;
		}
		m_RiseMask.GetComponent<AnimationTools>().sceneIndex = 2;
		m_RiseMask.gameObject.SetActive(value: true);
		if (ConstantData.onMaskRiseup != null)
		{
			ConstantData.onMaskRiseup();
		}
	}

	private void _GenerateTips(string text, Color c, Vector3 position, bool stationary)
	{
		TextTips textTip = GetTextTip();
		textTip.transform.position = position;
		textTip.SetText(text, c, stationary);
		DeployingTips(textTip);
	}

	private void DeployingTips(TextTips tip)
	{
		Vector3 localPosition = tip.transform.localPosition;
		for (int i = 0; i < m_TipsCache.Count; i++)
		{
			if (m_TipsCache[i] != null && m_TipsCache[i].gameObject.activeInHierarchy && tip.GetInstanceID() != m_TipsCache[i].GetInstanceID() && tip.rect.Overlaps(m_TipsCache[i].rect))
			{
				localPosition.y -= tip.rect.height + 1f;
				tip.transform.localPosition = localPosition;
				DeployingTips(tip);
			}
		}
	}

	private TextTips GetTextTip()
	{
		TextTips textTips = null;
		for (int i = 0; i < m_TipsCache.Count; i++)
		{
			if (m_TipsCache[i] != null && !m_TipsCache[i].gameObject.activeInHierarchy)
			{
				textTips = m_TipsCache[i];
				textTips.gameObject.SetActive(value: true);
				return textTips;
			}
		}
		textTips = SupportTools.AddChild<TextTips>(base.gameObject, m_Tips.gameObject);
		m_TipsCache.Add(textTips);
		return textTips;
	}

	public static void GenerateTips(string text, Color c, Vector3 position, bool stationary = false)
	{
		m_Instance._GenerateTips(text, c, position, stationary);
	}

	public static void GenerateTips(string text, Color c, bool stationary = false)
	{
		GenerateTips(text, c, Vector3.zero, stationary);
	}

	public static void GenerateTips(string text)
	{
		GenerateTips(text, Color.white);
	}

	private void pauseGame()
	{
		if (!AdManager1.instance.ShieldAddress)
		{
			AdManager.single.ShowNative();
		}
		Time.timeScale = 0f;
		pauseMenu.SetActive(value: true);
	}

	private void resumeGame()
	{
		Time.timeScale = 1f;
		pauseMenu.SetActive(value: false);
	}

	public void ClickShowLevelInfo()
	{
		TextDialog.Show(LevelDataIndex.CurrentLevel.DescriptionID);
	}
}
